package com.du.util;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.font.FontRenderContext;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.UUID;

import static java.awt.image.BufferedImage.TYPE_INT_ARGB;
import static java.awt.image.BufferedImage.TYPE_INT_RGB;

/**
 * Desc:
 * Author: dushuang
 * Date: 2020/11/10
 **/
public class PictureUtil {

    public static String PNG = "png";

    public static String JPG = "jpg";

   static Color[] colors = {
            new Color(180,10,255),
            new Color(230,10,10),
            new Color(5,150,60),
            new Color(255,50,135),
            new Color(255,110,50),
    };

   static Color[] twoColor(String content) {
       Color one = colors[Math.abs(content.hashCode()) % colors.length];
       Color two = new Color(one.getRed(), one.getGreen(), one.getBlue(), Math.abs(content.hashCode()) % 90);
       return new Color[]{one, two};
   }

    public static BufferedImage create(String content, Font font, int width, int height) {
        BufferedImage bi = new BufferedImage(width, height, TYPE_INT_RGB);
        Graphics2D g2 = (Graphics2D)bi.getGraphics();

        //************* 设置纯色背景 *****************//
//        g2.setBackground(new Color(180, 60, 255));
        g2.setBackground(colors[Math.abs(content.hashCode()) % colors.length]);
        g2.clearRect(0, 0, width, height);


        //************* 设置渐变背景 *****************//
        //设置渐变背景
//        BasicStroke stroke = new BasicStroke(width * 2);
//        g2.setStroke(stroke);
//        //设置渐变填充
//        Color[] color = twoColor(content);
//        GradientPaint gt = new GradientPaint(0, 0, color[0], (float) width/2 , (float) 0, color[1], true);
//        g2.setPaint(gt);
//        Line2D line = new Line2D.Float(0, 100, 100, 100);
//        g2.draw(line);

        //************* 设置名字 *****************//
        // 画名字
        g2.setPaint(Color.white);
        g2.setFont(font);
        FontRenderContext context = g2.getFontRenderContext();
        Rectangle2D bounds = font.getStringBounds(content, context);
        double x = (width - bounds.getWidth()) / 2;
        double y = (height - bounds.getHeight()) / 2;
        double ascent = -bounds.getY();
        double baseY = y + ascent;

        g2.drawString(content, (int)x, (int)baseY);
        return bi;
    }

    public static BufferedImage create(String content) {
        Font font = new Font("微软雅黑", Font.BOLD, 55);
        int width = 100;
        int height = 100;
        return create(content, font, width, height);
    }

    public static InputStream bufferedImageToInputStream(BufferedImage bi) {
        ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
        try {
            ImageIO.write(bi, JPG, arrayOutputStream);
            return new ByteArrayInputStream(arrayOutputStream.toByteArray());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) {
        BufferedImage bi = create("王");
        try {
            String name = UUID.randomUUID().toString() + ".jpg";
            ImageIO.write(bi, "JPG", new File("F:\\ds" + File.separator + name));
            System.out.println(String.format("图片 %s 已生成！", name));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
